var conditionSettingRoadLevelSpeed = {
	init : function() {
		$('.tab-alarm').height(nav.whiceH[1]);
		this.queryTabInit = this.quaryTab();//this.queryTabInit.reflash(0);
		this.choseCar();
		this.updateInit = this.saveRoadLevelSpeed();
		this.annal();
		this.allVehicleId="";
		this.allVehicleHtml="";//用来储存用户拥有的车辆
		this.initEvent();
		conditionSettingRoadLevelSpeed.alarmId = '';
		
	},loadUserVehicle :function(keyWord){
		
//加载所有权限的车辆
		$.ajax({
			type : 'post',
			async : false,  
			url : appCtx + "reportset/roadRoadLevelSpeed/loadUserVehicle.html",
			cache : false,
			data:{"keyWord":keyWord},
			dataType : "JSON",
			success : function(data) {
				if(data.vehicleList &&  data.vehicleList.length>0){
				var arr = data.vehicleList;
				var size = arr.length;
				conditionSettingRoadLevelSpeed.allVehicleId="";
				conditionSettingRoadLevelSpeed.vehicleArr=arr;
				conditionSettingRoadLevelSpeed.vehicleSize=size;
				var  html="";
				for (var i = 0; i < size; i++) {
					if (i == 0) {
						conditionSettingRoadLevelSpeed.allVehicleId+=arr[i].vehicleId+","
						html+='<li class="' + 'list active"><a class='
										+ '"db' + '" href=' + '"javascript:;'
										+ '">' + arr[i].registrationNO
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>';
					} else {
						conditionSettingRoadLevelSpeed.allVehicleId+=arr[i].vehicleId+","
						html+='<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ arr[i].registrationNO
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>'
					}
				}
				conditionSettingRoadLevelSpeed.allVehicleHtml=html;
				$(".queryCarCont").html(html);
			}
			}
		});
		
	},
	initEvent : function() {
		
		//车辆选择方式的控制(默认全选)
		 $("#allVehicle").on("click",function(){
	      	  var obj = document.getElementById("vehicleTab");
	            obj.style.display= "none";
	      })
	     $("#chooseVehicle").on("click",function(){
	      	  var obj = document.getElementById("vehicleTab");
	      	  obj.style.display= "block";
	     })
		
		
		$("#vehicleSearch").click(function() {
							var value = $("#queryValue").val();
							conditionSettingRoadLevelSpeed.loadUserVehicle(value);
						})
		$("#saveOrUpdateTab").click(function(){
			
			conditionSettingRoadLevelSpeed.alarmId = "";
			 $(".queryCarCont").html("");
			 conditionSettingRoadLevelSpeed.loadUserVehicle();
			$(".resultCarCont li div").text("");
			 $("#name").val("");
			 $("#allVehicle").attr("checked","checked");
			 //$("#speedThreshold").val("");
			 $("#remark").val("");
			 $(".resultCarCont li").remove();
			 var obj = document.getElementById("vehicleTab");
             obj.style.display= "none";
		 $(".queryCarCont").html(conditionSettingRoadLevelSpeed.allVehicleHtml);})

		$(".queryCarCont ").on("click", "li", function() {

			$(this).toggleClass("active");
		})
		$(".queryCarCont ").on("dblclick" ,"li",function() {
			var _this=this;
			$(this).remove();
			//$(this).addClass("active");
			$(".resultCarCont").append(this);

		})

		$(".resultCarCont").on("click", "li", function() {
			$(this).toggleClass("active");
		})
		$(".resultCarCont ").on("dblclick", "li", function() {
			var _this=this
			$(_this).remove();
			$(".queryCarCont").prepend(_this);

		})
		$(".go").click(function() {
			$(".resultCarCont").append($(".queryCarCont .active"));
			$(".queryCarCont .active").remove();
		})
		$(".back").click(function() {
			$(".queryCarCont").prepend($(".resultCarCont .active"));
			$(".resultCarCont .active").remove();
		})
   $("#saveOrUpdateTab").click(function(){
	   conditionSettingRoadLevelSpeed.alarmId = "";
	   
	   $("#queryValue").val("");
		 $(".queryCarCont").html("");
		
		 conditionSettingRoadLevelSpeed.loadUserVehicle();
	   
	   
	   $(".resultCarCont li div").text("");
		
		 $(".resultCarCont li div").text("");
		 $("#name").val("");
		 $("#analyse_groups_id").val("");
		 /*$("#speedThreshold").val("");*/
		 $("#remark").val("");
		 $(".resultCarCont li").remove();
 })
 $("#validateBtn").click(function(){
	$("velidateModal").modal("hide");
})
 $('#velidateModal').on('hidden', function() {
$("#content p").remove();
		})
	},

	quaryTab : function() {
		var index = 0;
		var _this = this;
		var func = function(){
			$('.tab-alarm-cont-list').hide().eq(index).show();
			$(this).addClass('active');
			$(this).siblings().removeClass('active');
			$('.date-alarm').find('.line').css({
				'left' : 100 * index
			});
		};
		$('.date-alarm').find('.list').click(function() {
			index = $(this).index();
			func();
		});
		return {
			'index' : index,
			reflash : function(i){
				index = i;
				func();
			}
		}
	},

	/**
	 * 车辆选择
	 */
	choseCar : function() {
		$('#selectCar').select2();
	},
	
	queryOtherVehicle:function(analyseGroupsId){
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleSpeed/queryOtherVehicle.html",
			cache : false,
			dataType : "JSON",
			 data:{"analyseGroupsId":analyseGroupsId,"analyseID":9},
			success : function(data) {
				console.log(data);
				 
				var arrOtherVehicle = data.data;
				var sizeOtherVehicle = arrOtherVehicle.length;
				var html="";
				for (var i = 0; i < sizeOtherVehicle; i++) {
					if (i == 0) {
						
						html+='<li class="' + 'list active"><a class='
										+ '"db' + '" href=' + '"javascript:;'
										+ '">' + arrOtherVehicle[i].VNAME
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arrOtherVehicle[i].VID + ',</div></li>'
					} else {
						html+='<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ arrOtherVehicle[i].VNAME
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arrOtherVehicle[i].VID + ',</div></li>'
					}
				}
				$(".queryCarCont").html(html);
			}
		});
	},
	
	
	updateRoadLevelSpeed : function(id,analyse_groups_id) {
		conditionSettingRoadLevelSpeed.alarmId = id;
		conditionSettingRoadLevelSpeed.queryTabInit.reflash(1);
		conditionSettingRoadLevelSpeed.queryOtherVehicle(analyse_groups_id);
		$(".queryCarCont").html("");
		conditionSettingRoadLevelSpeed.loadUserVehicle();
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + 'reportset/vehicleSpeed/queryVehicle.html',
			cache : false,
			data : {"id":analyse_groups_id},
			dataType : "JSON",
			success : function(data) {
				var arr=data.rows;
				var size=arr.length;
				var vehicleArr=conditionSettingRoadLevelSpeed.vehicleArr
				var vehicleSize=conditionSettingRoadLevelSpeed.vehicleSize
				$(".resultCarCont li").remove();
				/*//左边未选的车辆
				for (var i = 0; i < vehicleSize; i++) {
					if (i == 0) {
						
						$(".queryCarCont").append(
								'<li class="' + 'list active"><a class='
										+ '"db' + '" href=' + '"javascript:;'
										+ '">' + vehicleArr[i].registrationNO
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ vehicleArr[i].vehicleId + ',</div></li>')
						
					} else {
						$(".queryCarCont").append(
								'<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ vehicleArr[i].registrationNO
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ vehicleArr[i].vehicleId + ',</div></li>')
							
					}
				}*/
				for (var i = 0; i < size; i++) {
					console.log(arr[i]);
					if (i == 0) {
						$(".resultCarCont").append(
								'<li class="' + 'list active"><a class='
										+ '"db' + '" href=' + '"javascript:;'
										+ '">' + arr[i].registrationNo
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>')
					} else {
						$(".resultCarCont").append(
								'<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ arr[i].registrationNo
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>')
					}
				}
				
			}
			

		});
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/roadRoadLevelSpeed/updateRoadLevelSpeed.html",
			cache : false,
			data : {"id":id},
			dataType : "JSON",
			success : function(data) {
				var data=data.data;
				$("#name").val(data.name);
				/*$("#speedThreshold").val(data.speedThreshold);*/
				$("#remark").val(data.remark);
				 $("#chooseVehicle").attr("checked","checked");
				$("#analyse_groups_id").val(data.analyse_groups_id);
				var choose = document.getElementById("vehicleTab");
				choose.style.display= "block";
				/*$("#timeThreshold").val(data.timeThreshold);*/
			/*	$("#commandContent").val(data.commandContent);*/
				
				/*if(data.analyseType==1){
					$("#analyseTypeHeight").attr("checked","checked");
				}else{
					$("#analyseTypeLow").attr("checked","checked");
				}
				if(data.remindType==2){
					$("#remindType2").attr("checked","checked");
				}else if(remindType==3){
					$("#remindType3").attr("checked","checked");
				}else{
					$("#remindType4").attr("checked","checked");
				}
				if(data.is_weixin_intify==1){
					$("#wechart").attr("checked","checked")
					};
				if(data.is_sms_notify==1){
					$("#messager_notify").attr("checked","checked")
				}*/
				/*if("全部"==periodValue){
					
					$(".radio-warp").find("input")[0].checked=true;
				}else if("周一至周五"==periodValue){
					$(".radio-warp").find("input")[1].checked=true;
				}else if("周一至周六"==periodValue){
					$(".radio-warp").find("input")[2].checked=true;
				}else if("周六至周日"==periodValue){
					$(".radio-warp").find("input")[3].checked=true;
				}*/
				
			}
			

		});
		
		
		
	},
	saveRoadLevelSpeed : function() {
		
		var action = 'update';
		var param = {};
		//var periodValue;
		
		var require = function(param){
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "reportset/roadRoadLevelSpeed/saveRoadLevelSpeed.html",
				cache : false,
				data : param,
				dataType : "JSON",
				success : function(data) {
					$("#annal").flexReload();
					/*for (var i = 0; i < 4; i++) {
						if ($(".radio-warp").find("input")[i].checked) {
							$(".radio-warp").find("input")[i].checked=false;
						}
					}*/

					
					 global.ajaxLoading.pop(data.msg,false,2000);
					 
					
				}

			});
		};
		
		
		
		

		$("#saveRoadLevelSpeed").click(function() {
			
					/*for (var i = 0; i < 4; i++) {
						if ($(".radio-warp").find("input")[i].checked) {
							periodValue = $($(".radio-warp").find("input")[i])
									.parent("label").text();
						}
					}*/

					var ids = $(".resultCarCont li div").text();
					if(!ids){
						ids=conditionSettingRoadLevelSpeed.allVehicleId;
					}
					if($("#allVehicle").attr("checked")=="checked"){
						ids=conditionSettingRoadLevelSpeed.allVehicleId;
					}
					var name =$.trim($("#name").val());
					//var speedThreshold = $("#speedThreshold").val();
					var remark = $("#remark").val();
					var analyse_groups_id = $("#analyse_groups_id").val();
					
					if(!name){
						$("#content").append("  <p>请填写条件名称</p> ")
						$("#velidateModal").modal("show");
						return;
					}
					/*if(!speedThreshold){
						$("#content").append("  <p>请填速度门限</p> ")
						$("#velidateModal").modal("show");
						return;
					}*/
					/*var timeThreshold=$("#timeThreshold").val();
					if(!timeThreshold){
						timeThreshold=360;
					}*/
					/*var analyseType;
					if($("#analyseTypeHeight").attr("checked")=="checked"){
						 analyseType=1;
					}else{
						 analyseType=2;
					}*/
					/*var commandContent=$("#commandContent").val();
					if(!commandContent ){
						commandContent="您已超速，请减速行驶!";
					}*/
					/*var remindType=2;
					if($("#remindType3").attr("checked")=="checked"){
						remindType=3;
					}
					if($("#remindType4").attr("checked")=="checked"){
						remindType=4;
					}
					var wechart=0;
					if($("#wechart").attr("checked")=="checked"){
						 wechart=1
					}
					var messager_notify=0;
					if($("#messager_notify").attr("checked")=="checked"){
						messager_notify=1
					}*/
						
					
					param =  {
							
						"Name" : name,
						/*"SpeedThreshold" : speedThreshold,*/
						"PeriodValue" : "每日",
						"Remark" : remark,
						"ids" : ids,
						"action" : action,
						"analyse_groups_id":analyse_groups_id,
						/*"TimeThreshold":timeThreshold,
						"CommandContent":commandContent,
						"AnalyseType":analyseType,
						"RemindType":remindType,
						"Is_weixin_notify":wechart,
						"Is_sms_notify":messager_notify,*/
						"id":conditionSettingRoadLevelSpeed.alarmId
					} 
					func();
					conditionSettingRoadLevelSpeed.queryTabInit.reflash(0);
				});
		var func=function(){
			if(!param.ID){
				action="insert";
			}
			param.action = action;
			require(param);
		};
		
      return{
    	  updateRoadLevelSpeed : function(action){
			func(action);
		}
     }

	},
	/**
	 * 删除超速设置
	 */

	deleteRoadLevelSpeed : function(id,analyse_groups_id) {
		$("#comDelBtn").click(function(){
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "reportset/roadRoadLevelSpeed/deleteRoadLevelSpeed.html",
				cache : false,
				data : {
					"id" : id,
					"analyse_groups_id":analyse_groups_id
				},
				dataType : "JSON",
				success : function(data) {
					
					
				}

			});
			$("#myModalDel").modal("hide");
			$.ajax({
				type : 'post',
				async : true,
				url : appCtx + "reportset/roadRoadLevelSpeed/deleteRoadLevelSpeedVehicle.html",
				cache : false,
				data : {
					
					"analyse_groups_id":analyse_groups_id
				},
				dataType : "JSON",
				success : function(data) {
					
					
				}

			});
			$("#annal").flexReload();
		})
		

	},
	queryVehicle : function(id) {

		$('#myModal').on('hidden', function() {
			$(".ownCarCont li").remove();
		})
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleSpeed/queryVehicle.html",
			cache : false,
			data : {
				"id" : id
			},
			dataType : "JSON",
			success : function(data) {
				var vehicleArr=data.rows;
				var size=vehicleArr.length;
				console.log(vehicleArr);
				for (var i = 0; i < size; i++) {
					console.log(vehicleArr[i].registrationNo)
						$(".ownCarCont").append(
								'<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ vehicleArr[i].registrationNo
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ vehicleArr[i].vehicleId + ',</div></li>')
				}
			}

		});
		
		
	},
	annal : function() {
		/**
		 * 操作列
		 */
		function formmatOperation(id,analyse_groups_id) {
			var handleStr = "";
			handleStr = '<a href="javascript:void(0)" onclick="conditionSettingRoadLevelSpeed.updateRoadLevelSpeed('
					+ id+','+analyse_groups_id
					+ ')" title="" ><span style='+'"color: blue;'+'">修改</span></a>&nbsp;&nbsp;<a href="#myModalDel" onclick="conditionSettingRoadLevelSpeed.deleteRoadLevelSpeed('
					+ id+','+analyse_groups_id + ')" '+'data-toggle="'+'modal"'+'title="'+'" ><span style='+'"color: blue;'+'">删除</span></a>';

			return handleStr;
		}
		;
		function formmatVehicle(analyse_groups_id) {
			var handleStr = "";
			handleStr = '<a href="' + '#myModal' + '"  role=' + '"button'
					+ '" onclick=' + '"conditionSettingRoadLevelSpeed.queryVehicle('
					+ analyse_groups_id + ')" data-toggle=' + '"modal'
					+ '"><span style=' + '"color: blue;' + '"</span>查看车辆</a>';
			return handleStr;
		};

		$("#annal").flexigrid({
			dataType : 'json',
			url : appCtx + 'reportset/roadRoadLevelSpeed/queryAllRoadLevelSpeed.html',
			params : [],
			contentType : "application/json; charset=utf-8",
			method : 'POST',
			widthType:'%',
	        width:$('.tab-alarm-cont').width(),
			colModel : [ {
				display : '条件名称',
				name : 'name',
				width : 30,
				sortable : false,
				// hide:true,
				align : 'left',
				checked : 'checked'
			}, {
				display : '备注',
				name : 'remark',
				width : 30,
				sortable : false,
				align : 'left',
			}, {
				display : '监控车辆',
				name : 'Handler',
				width : 20,
				sortable : false,
				align : 'center',

				paramcolnames : [ 'analyse_groups_id' ],
				handlefunction : formmatVehicle,
			}, {
				display : '操作',
				name : 'operation',
				width : 20,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'id','analyse_groups_id' ],
				handlefunction : formmatOperation
			} ],

			resizable : false,
			sortname : "terminalId",// 第一次加载数据时排序列
			sortorder : "desc",// 第一次加载数据时排序类型
			usepager : false,// 是否分页，默认为true。
			showToggleBtn : false,
			showTableToggleBtn : false,// 是否显示收起/打开按钮,默认不显示。
			// useRp : true,// 是否可以动态设置每页显示的结果数，默认为false。
			// rp : 10,// 每页记录数，默认为10
			// rpOptions: [10,50,100,300,500], // 每页结果数
			checkbox : true,// 是否要多选框,默认为false。
			rowId : 'terminalId',// 多选框绑定行的id,只有checkbox :
			// true时才有效。
			singleSelect : true,
			height : nav.whiceH[1] - 130, // 表格高度
			titDisplay : false,
			onSuccess : function(data) {
			},
			onSelectRow : function(row) {

			},
			onSelectAllRow : function(data) {

			}

		});
	}
};

conditionSettingRoadLevelSpeed.init();